[Notes] Java Servlet

Servlet

REST(Representational State Transfer)

  • REST is web server design pattern
  • Operations are directly based on HTTP methods, so that server don’t need to parse extra thing
  • URL clearly indicates which resource a client want, easy for client side users to understand.
  • Server is running in stateless mode, improve scalability.

Create Jupyter Project && SearchItem Servlet

  • Create Dynamic Project called “Jupiter”
  • Add Tomcat 9.0 to its Java Build Path
  • Add And Remove project Jupyter to existed Tomcat server
  • Create java Servlet in Jupyter project, name it SearchItem and name the package rpc
  • It should contain a doGet() and doPost()
  • change the mapping url

    1
    2
    3
    4
    // better style to set them all lowercase
    // 可以直接在创建servlet的页面修改
    @WebServlet("/SearchItem") // by default
    @WebServlet("/search")
  • Handle the doGet() method by adding tutorial code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // response.getWriter()表示得到response的输出流
    PrintWriter out = response.getWriter();

    // request.getParameter表示获取request中的body的参数信息
    if (request.getParameter("username") != null) {
    String username = request.getParameter("username");
    out.print("Hello " + username);
    }

    // 一定要关闭这个输出流
    out.close();
  • test the input

    1
    http://localhost:8080/Jupiter/search?username=abcd
  • return a HTML page

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 需要告诉浏览器页面的类型,渲染成所需的格式
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<html><body>");
    out.println("<h1>This is a HTML page</h1>");
    out.println("</body></html>");

    out.close();
  • add Java-json jar support to the WebContent-WebInf-lib

  • test json support

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 把页面设置成json键值对格式
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();

    String username = "";
    if (request.getParameter("username") != null) {
    username = request.getParameter("username");
    }
    JSONObject obj = new JSONObject();
    try {
    obj.put("username", username);
    } catch (JSONException e) {
    e.printStackTrace();
    }
    out.print(obj);
    out.close();
  • test jason array

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    //json array 存储多个键值对
    JSONArray array = new JSONArray();
    try {
    array.put(new JSONObject().put("username", "abcd"));
    array.put(new JSONObject().put("username", "1234"));
    } catch (JSONException e) {
    e.printStackTrace();
    }
    out.print(array);
    out.close();

Create RecommendItem Servlet

  • Create another Servlet called RecommendItem under prc package, and change the mapping url
  • practice on JSONArray
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 1. set content type
    response.setContentType("application/json");
    // 2. get output stream of response
    PrintWriter out = response.getWriter();
    // 3. create json array
    JSONArray arr = new JSONArray();
    try {
    // 4. create JSONObject and put those in the JSONArray
    arr.put(new JSONObject().put("name", "abcd").put("address", "san francisco").put("time","01/01/2017"));
    arr.put(new JSONObject().put("name", "1234").put("address", "san jose").put("time","01/02/2017"));
    } catch (JSONException e) {
    e.printStackTrace();
    }
    // 5. print the result
    out.print(arr);
    out.close();

Add RpcHelper utility class

  • create regular class RpcHelper under rpc package
  • add writeJSONObject(response, JSONObject)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static void writeJSONObject(HttpServletResponse response , JSONObject obj) {
    try {
    // 1. 修改content type
    response.setContentType("application/json");
    // 2. 加入header response.addHeader
    response.addHeader("Access-Control-Allow-Origin", "*");
    // 3. 获取输出流 PrintWriter
    PrintWriter out = response.getWriter();
    // 4. 写入
    out.print(obj);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
  • add writeJSONArray(response, JSONArray)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static void writeJSONArray(HttpServletResponse response , JSONArray array) {
    try {
    // 1. 修改content type
    response.setContentType("application/json");
    // 2. 加入header
    response.addHeader("Access-Control-Allow-Origin", "*");
    // 3. 获取输出流
    PrintWriter out = response.getWriter();
    // 4. 写入
    out.print(array);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
  • now apply the helper to the SearchItem and RecommendationItem

TicketMaster API

  • 2. JavaServlet-2019-4-11-11-24-33.png
  • An web based API provided by TicketMaster so that clients and get real events data from TicketMaster server. You cannot see the source code of it, but you can refer to the documentation to figure out how to use the interface to make connection by sending request to it’s backend.
  • It’s required by TicketMaster API so that it’ll know who is using their API. To get your API key, you need to register on this website: http://developer.ticketmaster.com, and you’ll see your API key when we navigate to API details.
  • The bottle neck of this project is TicketMaster API, since we cannot control its performance.
  • If we have to optimize it, we can create a cache.
  • Or we can make another API. Make a copy each time we query something from TicketMaster.
  • If TicketMaster is down. We can query local copy of nearest geo point.模仿一个曲线拟合的过程,即模糊搜索